package com.example.shengyin.controller;

import com.alibaba.fastjson2.JSON;
import com.example.shengyin.entity.Appoint;
import com.example.shengyin.entity.Page;
import com.example.shengyin.entity.User;
import com.example.shengyin.service.AppointService;
import com.example.shengyin.util.Result;
import com.example.shengyin.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

@RestController
@RequestMapping("/appoint")
@Validated
@CrossOrigin(origins = "*", maxAge = 3600,allowCredentials="true")
//swagger
@Api("预约")
public class AppointController {
    @Autowired
    private AppointService service;
    @Autowired
    private RedisTemplate redisTemplate;

    @ApiOperation("预约列表")
    @GetMapping("/search")

    public Result event(@ApiParam(name = "uname",value = "预约人名") @RequestParam(required = false) String uname,
                      @RequestParam(name = "page",defaultValue = "1") int page,
                      @RequestParam(name = "limit",defaultValue = "10")int limit) {
        Page p = service.selectList(uname, page, limit);
        int count = service.fand(uname);
        if(count==0||p==null)
            return Result.error("没有数据");
        return Result.success(p);
    }

    @GetMapping(value = "/select/{id}")
    public Result select(@PathVariable(value = "id") Long id)
    {
        Appoint select = service.selectById(id);
        if(select==null)
            return Result.error("没有找到数据");
        return Result.success(select);
    }
    @ApiOperation("增加预约列表")
    @PostMapping("/add")
    public Result insert(@Valid  Appoint appoint, HttpServletRequest request)
    {
        System.out.println(request.getHeader("Authorization"));
        User user = (User) redisTemplate.opsForValue().get(request.getHeader("Authorization"));
        System.out.println("out"+ JSON.toJSONString(appoint));
        appoint.setUserid((user.getId()));
        int insert = service.add(appoint);
        if(insert==0)
            return Result.error("添加失败");
        return Result.success("添加成功");
    }

    @ApiOperation("修改预约列表")
    @PostMapping("/update")
    public Result update(Appoint appoint)
    {
        int insert = service.update(appoint);
        if(insert==0)
            return Result.error("修改失败");
        return Result.success("修改成功");
    }

    @GetMapping("/delete/{id}")
    public Result delete(@PathVariable(value = "id")int id)
    {
        int delete = service.delById(id);
        if(delete==0)
            return Result.error("删除失败");
        return Result.success("删除成功");
    }
    @GetMapping("/deleteByIds/{ids}")
    public Result deleteByIds(@PathVariable(value = "ids")String ids)
    {
        System.out.println("开始这些");
        String s = StrUtil.ListtoString(ids);
        int delete = service.delByIds(s);
        if(delete==0)
            return Result.error("删除失败");
        return Result.success("删除成功");
    }

}
